//
//  MXHomeWashItemDetailVC.m
//  MXの衣柜
//
//  Created by 马霄 on 15/8/11.
//  Copyright (c) 2015年 霄 马. All rights reserved.
// 洗护详情页 选择商品页
#define WIDTH ([UIScreen mainScreen].bounds.size.width)
#define HEIGHT ([UIScreen mainScreen].bounds.size.height)
#import "MXHomeWashItemDetailVC.h"
#import "AFNetworkHandler.h"
#import "MXHomeWashDetailModel.h"
#import "MXHomeContentCollectionViewCell.h"
#import "MXHomeWebViewVC.h"
#import "MJRefresh.h"
#import "MBProgressHUD.h"
@interface MXHomeWashItemDetailVC ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, retain) UICollectionView *collectionView;

@property (nonatomic, retain) NSMutableArray *washItemArray;

@property (nonatomic, retain) NSString *TmallLinkString;

@property (nonatomic, assign) NSInteger pageNum;

@property (nonatomic, retain) MBProgressHUD *HUD;

@property (nonatomic, retain) UIView *alterView;
@end

@implementation MXHomeWashItemDetailVC
- (void)dealloc
{
    [_HUD release];
    [_alterView release];
    [_TmallLinkString release];
    [_washItemArray release];
    [_titleString release];
    [_washID release];
    [_collectionView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // 全局数组初始化
    self.washItemArray = [NSMutableArray array];
    // 标题Label
    {
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH,HEIGHTPRO * 64)];
    titleLabel.backgroundColor = [UIColor whiteColor];
    titleLabel.text = @"";
    titleLabel.userInteractionEnabled = YES;
    [self.view addSubview:titleLabel];
    [self.view addSubview:titleLabel];
    
    UIButton *backButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    backButton.frame = CGRectMake(WIDTHPRO * 10,HEIGHTPRO * 29,WIDTHPRO * 30,HEIGHTPRO * 30);
    [backButton setBackgroundImage:[UIImage imageNamed:@"backButton_blue"] forState:(UIControlStateNormal)];
    [backButton addTarget:self action:@selector(backAction) forControlEvents:(UIControlEventTouchUpInside)];
    [titleLabel addSubview:backButton];
    
        
        
    UILabel *titleWord = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 2 - WIDTHPRO * 60, HEIGHTPRO * 24, WIDTHPRO * 120,HEIGHTPRO * 40)];
    titleWord.text = self.titleString;
    titleWord.textAlignment = NSTextAlignmentCenter;
    titleWord.font = [UIFont systemFontOfSize:16];
    [titleWord setTextColor:[UIColor blueColor]];
    [titleLabel addSubview:titleWord];
    }
    
    [self creatCollectionView];

    self.pageNum = 2;
    
    // 引入数据部分
    [self GetData];
    
    // 添加刷新方法
    [self loadingCollectionView];
    
    [self refreshTableView];

    // Do any additional setup after loading the view.
}
-(void)uiActivityindicatorviewStart
{
    self.alterView =[[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)] autorelease];
    self.alterView.backgroundColor =[UIColor whiteColor];
    self.HUD = [MBProgressHUD showHUDAddedTo:self.alterView animated:YES];
//    self.HUD.labelText = @"正在努力的给您加载";
    [self.HUD show:YES];
    
    [self.view addSubview:self.alterView];
    
}
-(void)uiActivityindicatorviewEnd
{
    [self.alterView removeFromSuperview];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - 创建集视图
- (void)creatCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    flowLayout.itemSize = CGSizeMake(WIDTHPRO * 170,HEIGHTPRO * 240);
    flowLayout.minimumLineSpacing = 10;
    flowLayout.sectionInset = UIEdgeInsetsMake(WIDTHPRO * 10 ,HEIGHTPRO * 10 , 0,HEIGHTPRO * 10);
    
    self.collectionView = [[[UICollectionView alloc] initWithFrame:CGRectMake(0,WIDTHPRO * 64, WIDTH, HEIGHT  - HEIGHTPRO * 64) collectionViewLayout:flowLayout] autorelease];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.collectionView];
    
    
    [self.collectionView registerClass:[MXHomeContentCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
    [self uiActivityindicatorviewStart];
    
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(uiActivityindicatorviewEnd) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

}

#pragma mark -- 下拉刷新
-(void)refreshTableView
{
    //    __block MXFindVC *findVC = self;
    [self.collectionView addHeaderWithCallback:^{
        //        [self GetFindData];
        [self.collectionView headerEndRefreshing];
    }];
    //添加下拉刷新头部控件
    
}

#pragma mark -- 上拉加载

-(void)loadingCollectionView
{
    
    //    __unsafe_unretained typeof(self) vc = self;
    [self.collectionView addFooterWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSString *washPath = [NSString stringWithFormat:@"http://api.nanyibang.com/daily-items?kind_id=%@&page=%ld", self.washID, (long)self.pageNum];
            self.pageNum += 1;
            [AFNetworkHandler GETwithAFNByURL:washPath completion:^(id result) {
                NSArray *dataArr = [result objectForKey:@"data"];
                for (NSDictionary *dict in dataArr) {
                    MXHomeWashDetailModel *model = [[[MXHomeWashDetailModel alloc] initWithDic:dict] autorelease];
                    [self.washItemArray addObject:model];
                }
            }];

            [self.collectionView reloadData];
            [self.collectionView footerEndRefreshing];
        });
    }];
}


/************************* 解析数据部分 **************************/
- (void)GetData
{
    NSString *washPath = [NSString stringWithFormat:@"http://api.nanyibang.com/daily-items?kind_id=%@&page=1", self.washID];
    [AFNetworkHandler GETwithAFNByURL:washPath completion:^(id result) {
        
        [self.washItemArray removeAllObjects];
        
        NSArray *dataArr = [result objectForKey:@"data"];
        for (NSDictionary *dict in dataArr) {
            MXHomeWashDetailModel *model = [[[MXHomeWashDetailModel alloc] initWithDic:dict] autorelease];
            [self.washItemArray addObject:model];
        }
        [self.collectionView reloadData];
        [self uiActivityindicatorviewEnd];

    }];
}
#pragma mark - CollectionView 协议实现部分
/************************* CollectionView Delegate **************************/
//Collection中的组别个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//每个组别中元件的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.washItemArray.count;
}
//重用机制
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MXHomeContentCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    [cell getWashInfo:self.washItemArray[indexPath.row]];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MXHomeWebViewVC *webViewVC = [[[MXHomeWebViewVC alloc] init] autorelease];
    webViewVC.requestString = [self.washItemArray[indexPath.row] purchaseLink];
    [self presentViewController:webViewVC animated:YES completion:^{
        
    }];
}
/************************* 按钮点击方法 **************************/
- (void)backAction
{
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
